package com.swf.seed.os.command.impl;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.net.util.Base64;

import com.swf.seed.base.SdCharset;
import com.swf.seed.base.SdLetter;
import com.swf.seed.base.SystemContants;
import com.swf.seed.os.annotation.Command;
import com.swf.seed.security.des.Security3DESSigner;

/**
 * <p>
 * 加密解密文件或者字符串
 * </p>
 * @author Weifeng Shen
 * @since 2016年7月22日
 * @see
 */
@Command("3des")
public class Des3Command extends CommandAdaptor{

	@Override
	public void option() {
		StringBuilder builder = new StringBuilder();
		builder.append("Description:").append(SystemContants.LINE_SEPARATOR);
		builder.append("  supported algorithm:3DES").append(SystemContants.LINE_SEPARATOR);
		builder.append("Example:").append(SystemContants.LINE_SEPARATOR);
		builder.append("  加密字符串：3des -m e -c GBK -s abcd -k 123456").append(SystemContants.LINE_SEPARATOR);
		builder.append("  解密字符串：3des -m d -c GBK -s kWLEe5I3d4c= -k 123456").append(SystemContants.LINE_SEPARATOR);
		builder.append("  加密文件:3des -m e  -c GBK -f d:/linux/test/a.txt d:/linux/test/b.txt -k 123456").append(SystemContants.LINE_SEPARATOR);
		builder.append("  解密文件:3des -m d  -c GBK -f d:/linux/test/b.txt d:/linux/test/c.txt -k 123456").append(SystemContants.LINE_SEPARATOR);
		footer = builder.append(super.footer);
		options.addOption(Option.builder(SdLetter.c).longOpt("charset").argName("charset" )
				.numberOfArgs(1).desc("charser" ).build());  
		options.addOption(Option.builder(SdLetter.s).longOpt("str").argName("string" )
				.numberOfArgs(1).desc("string to sign" ).build()); 
		options.addOption(Option.builder(SdLetter.m).longOpt("method").argName("method" )
				.numberOfArgs(1).desc("d mean decode and decrypt,e mean encode and encrypt" ).required().build()); 
		options.addOption(Option.builder(SdLetter.k).longOpt("key").argName("key" )
				.numberOfArgs(1).desc("key" ).build()); 
		options.addOption(Option.builder("f").longOpt("file").numberOfArgs(2).argName("<srcFile,destFile>" )
				.desc("srcFile is the source File,destFile is the destination File" ).build()); 
	}

	@Override
	void customizedExecute(CommandLine commandLine) throws Exception {
		String algorithm = commandLine.getOptionValue(SdLetter.a);
		String charset = commandLine.getOptionValue(SdLetter.c);
		if (charset==null){
			charset = SdCharset.defaultCharset;
		}
		String method = commandLine.getOptionValue(SdLetter.m);
		String key = commandLine.getOptionValue(SdLetter.k);
		byte[] bkey = Security3DESSigner.build3DesKey(key,charset);
		if (commandLine.hasOption(SdLetter.f)){
			String srcFile = commandLine.getOptionValues(SdLetter.f)[0];
			String destFile = commandLine.getOptionValues(SdLetter.f)[1];
			if ("d".equalsIgnoreCase(method)){
				Security3DESSigner.decrypt(srcFile,destFile,bkey);
			}else{
				Security3DESSigner.encrypt(srcFile,destFile,bkey);
			}
		}else{
			String str = commandLine.getOptionValue(SdLetter.s);
			if ("d".equalsIgnoreCase(method)){
				System.out.println(new String(Security3DESSigner.decryptBy3DES(Base64.decodeBase64(str), bkey),charset));
			}else{
				System.out.println(Base64.encodeBase64String(Security3DESSigner.encryptBy3DES(str.getBytes(charset), bkey)));
			}
		}
	}
}
